package com.example.administrator.readbook.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.readbook.R;
import com.example.administrator.readbook.bean.ChapterListBean;

import java.util.List;


/**
 * Created by Administrator on 2018/7/3.
 */

public class AdapterChapterList extends BaseAdapter {
    private Context context;
    private List<ChapterListBean.ChaptersBean> chapterList;
    private String readChapter;

    public AdapterChapterList(Context context, List<ChapterListBean.ChaptersBean> chapterList,String readChapter) {
        this.context = context;
        this.chapterList = chapterList;
        this.readChapter = readChapter;
    }

    @Override
    public int getCount() {
        return chapterList.size();
    }

    @Override
    public Object getItem(int position) {
        return chapterList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_chapterlist, null);
            viewHolder = new ViewHolder();
            viewHolder.titleTv = convertView.findViewById(R.id.adapter_chapterlist_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (String.valueOf(chapterList.get(position).getTitle()).equals(readChapter)) {
            viewHolder.titleTv.setTextColor(context.getResources().getColor(R.color.red));
        } else {
            viewHolder.titleTv.setTextColor(Color.GRAY);
        }
        viewHolder.titleTv.setText(chapterList.get(position).getTitle());
        return convertView;
    }

    public void setReadChapter(String readChapter) {
        this.readChapter = readChapter;
        notifyDataSetChanged();
    }

    class ViewHolder{
        TextView titleTv;
    }
}
